package cn.itsource.pethome.system.menu.controller;

import cn.itsource.pethome.system.dept.domain.Department;
import cn.itsource.pethome.system.menu.service.IMenuService;
import cn.itsource.pethome.system.menu.domain.Menu;
import cn.itsource.pethome.system.menu.query.MenuQuery;
import cn.itsource.pethome.utils.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.add(menu);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
                menuService.delete(id);
            return  AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setSuccess(false).setMsg("操作失败");
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Menu findnoe = menuService.findnoe(id);
            return AjaxResult.success().setData(findnoe);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Menu> list(){

        return menuService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult findQuey(@RequestBody MenuQuery query)
    {
        try {
            List<Menu> queryData = menuService.queryData(query);
            Long count = menuService.queryCount(query);
            return AjaxResult.success().setData(queryData).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
    @PatchMapping("/del")
    @ApiOperation(value = "根据id批量删除菜单")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            menuService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }
    @GetMapping("/tree")
    @ApiOperation(value = "返回部门树")
    public List<Menu> getTree(){
        return menuService.getTree();
    }
}
